package com.cuiweiyou.uiautomator20;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ManagerViewID {
    public static final String VIEW_ID_NAME_home_tab_item = "home_tab_item";// com.tencent.mm:id/com.tencent.mm:id/cw5 // 底部tab每个item的id

    public static final String VIEW_ID_NAME_home_wechat_tab = "home_wechat_tab";// com.tencent.mm:id/cw4      // 首页微信右上角数字
    public static final String VIEW_ID_NAME_home_wechat_list_item = "home_wechat_list_item";// com.tencent.mm:id/lv // 首页聊天列表，好友头像右上角数字

    public static final String VIEW_ID_NAME_chat_title = "chat_title";// com.tencent.mm:id/j7            // 聊天，聊天标题，好友的备注名称谓
    public static final String VIEW_ID_NAME_chat_list_item = "chat_list_item";// com.tencent.mm:id/a6        // 聊天，聊天记录item，对方的消息或我发出的消息或系统消息
    public static final String VIEW_ID_NAME_chat_list_item_avatar = "chat_list_item_avatar";// com.tencent.mm:id/mp // 聊天消息，好友的头像
    public static final String VIEW_ID_NAME_chat_list_item_msg = "chat_list_item_msg";// com.tencent.mm:id/mr    // 聊天消息，文本控件

    public static final String VIEW_ID_NAME_chat_edit_bar_txt = "chat_edit_bar_txt";// com.tencent.mm:id/aif      // 聊天，底部文本输入框
    public static final String VIEW_ID_NAME_chat_edit_bar_audio = "chat_edit_bar_audio";// com.tencent.mm:id/aid    // 聊天，底部语音、文本模式切换按钮

    public static final String VIEW_ID_NAME_search_wechat_qq_phone_keyword = "search_wechat_qq_phone_keyword";// 微信号/QQ号/手机号    // 查找，在”添加朋友“界面，不同微信版本关键字不一样
    /**
     * com.tencent.mm:id/jh  <br/>
     * 查找，输入号码
     */
    public static final String VIEW_ID_NAME_search_edittext_input = "search_edittext_input";
    /**
     * com.tencent.mm:id/do1         <br/>
     * 查找，发送时的打招呼
     */
    public static final String VIEW_ID_NAME_search_sayhello = "search_sayhello";//
    public static final String VIEW_ID_NAME_search_notename = "search_notename";// com.tencent.mm:id/do5             // 查找，发送时，设置备注名
    public static final String VIEW_ID_NAME_search_contacts_letter_list = "search_contacts_letter_list";// com.tencent.mm:id/lm  // 查找，通讯录页，右侧字母滑条
    public static final String VIEW_ID_NAME_search_contacts_friend_new_mask = "search_contacts_friend_new_mask";// com.tencent.mm:id/bm6  // 查找，通讯录页，顶部原先的”新的朋友“，当有消息时会被遮挡

    public static final String VIEW_ID_NAME_search_the_search_btn = "search_the_search_btn";// com.tencent.mm:id/nj  // 查找，输入号码，动态出现的搜索按钮，搜索:xxxx
    public static final String VIEW_ID_NAME_search_the_search_btn_p = "search_the_search_btn_p";// com.tencent.mm:id/r_  // 查找，输入号码，动态出现的搜索按钮，搜索:xxxx，父控件

    public static final String VIEW_ID_NAME_note_and_label_set_notename = "note_and_label_set_notename";// 设置备注和标签页面，备注名，EditText

    public static final String VIEW_ID_NAME_detail_name_note = "detail_name_note";// com.tencent.mm:id/u1  // 朋友的个人详细信息页，上面第一个，备注名
    public static final String VIEW_ID_NAME_detail_name_nick = "detail_name_nick";// com.tencent.mm:id/ax3 // 朋友的个人详细信息页，中间一个，昵称，带有"昵称"
    public static final String VIEW_ID_NAME_detail_name_wxid = "detail_name_wxid";// com.tencent.mm:id/ax3 // 朋友的个人详细信息页，下面一个，微信号或啥的

    private static ManagerViewID instance;

    private Map<String, Map<String, String>> deviceMap;
    private Map<String, String> selfViewIdKVMap;

    private ManagerViewID() {
        deviceMap = new HashMap<>();
    }

    public static ManagerViewID getInstance() {
        if (null == instance) {
            synchronized (ManagerViewID.class) {
                if (null == instance) {
                    instance = new ManagerViewID();
                }
            }
        }
        return instance;
    }

    public void initSelf(String deviceSN, String versionName) {
        init();

        String deviceAndWx = deviceSN.toLowerCase() + "===" + versionName; // 手机SN===微信版本
        Map<String, String> viewIdKVMap = deviceMap.get(deviceAndWx);
        if (null == viewIdKVMap || viewIdKVMap.size() < 1) {
            return;
        }

        this.selfViewIdKVMap = viewIdKVMap;
    }

    private void init() {
        File deviceFile = UtilFile.getDeviceFile();
        if (!deviceFile.exists()) {
            UtilLogs.saveLog("没有device文件");
            return;
        }

        try {
            InputStream is = new FileInputStream(deviceFile);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            List<String> group = new ArrayList<>();
            while (null != (line = reader.readLine())) {
                line = line.trim();
                if (line.length() < 1) {
                    continue;
                }
                if (line.startsWith("###")) { // 新的一句
                    saveDevicesViewId(group);
                    group.clear();
                }
                group.add(line);
            }

            saveDevicesViewId(group);
            group.clear();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void saveDevicesViewId(List<String> group) {
        if (group.size() < 1) {
            return;
        }

        String title = group.get(0);
        String deviceSnAndWxVersion = title.replace("###", "").toLowerCase(); // ###开头的是设备分组。手机SN===微信版本

        Map<String, String> map = deviceMap.get(deviceSnAndWxVersion);
        if (null == map) {
            map = new HashMap<>();
            deviceMap.put(deviceSnAndWxVersion, map);
        }

        for (int i = 1; i < group.size(); i++) {
            String keyLine = group.get(i);
            String[] split = keyLine.split("==="); // 右边的是注释
            String keyValue = split[0].trim();
            String[] split2 = keyValue.split("="); // key=value的形势
            String view_id_key = split2[0];
            String view_id_value = split2[1];

            UtilLogs.saveLog("设备微信：" + deviceSnAndWxVersion + "，View：" + view_id_key + "，ID：" + view_id_value);

            map.put(view_id_key, view_id_value);
        }
    }

    public String getSelfViewId(String viewidKey) {
        if (0 == 0) {
            return null; // todo 临时关闭
        }
        if (null == selfViewIdKVMap) {
            return null;
        }

        String viewid = selfViewIdKVMap.get(viewidKey);

        UtilLogs.saveLog("取动态ViewID key：" + viewidKey);
        UtilLogs.saveLog("取动态ViewID value：" + viewid);

        return viewid;
    }
}
